<template>
  <div class="album-container">
    <!-- 专辑信息 -->
    <div class="album-info">
      <!-- 专辑封面 -->
      <el-image style="width: 182px; height: 182px; border-radius: 6px" v-if="albumCon.picUrl !== undefined" :src="albumCon.picUrl" alt=""></el-image>
      <!-- 专辑名称 -->
      <div class="name-box">
        <span class="type">专辑</span>
        <h2>{{ albumCon.name }}</h2>
      </div>
      <!-- 播放全部 -->
      <div class="play-all" @click="open">
        <i class="el-icon-caret-right"></i>
        <span>播放全部</span>
        <i class="el-icon-plus"></i>
      </div>
      <!-- 歌手 -->
      <div class="artist">
        歌手：
        <span>{{ artist }}</span>
      </div>
      <!-- 时间 -->
      <div class="time">
        时间：
        <span>{{ albumCon.publishTime | conversion('yyyy-MM-dd') }}</span>
      </div>
    </div>
    <!-- 歌曲列表 -->
    <div class="album-songlist">
      <el-table :data="albumSongList" stripe style="width: 100%">
        <el-table-column type="index" width="50" align="right"></el-table-column>
        <el-table-column prop="name" label="音乐标题" min-width="40%">
          <template slot-scope="scope">
            <span style="display: inline-block; width: 100%" @click="chooseSong(scope.row.id)">{{ scope.row.name }}</span>
            <i class="el-icon-star-on" :class="isLoved(scope.row.id)" @click="myLove(scope.row)"></i>
          </template>
        </el-table-column>
        <el-table-column prop="ar" label="歌手" min-width="20%" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-for="item in scope.row.ar" :key="item.id">{{ item.name }}{{ item.name !== scope.row.ar[scope.row.ar.length - 1].name ? ' / ' : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="al.name" label="专辑" min-width="20%" show-overflow-tooltip></el-table-column>
        <el-table-column prop="dt" label="时长" min-width="10%">
          <template slot-scope="scope"> {{ scope.row.dt | conversion('mm:ss') }}</template>
        </el-table-column>
        <el-table-column prop="pop" label="热度" min-width="10%">
          <template slot-scope="scope">
            <el-progress :percentage="scope.row.pop" color="#CDCDCD" :show-text="false"></el-progress>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getAlbumConByIdAPI } from '@/api/playListAPI.js'
import { savePlayList, clearPlayList, saveMyLoveSongList, loadMyLoveSongList, deleteMyLoveSongList } from '@/js/cache'

import bus from '@/components/eventBus.js'

export default {
  name: 'Album',
  data() {
    return {
      id: this.$route.query.id,
      // 专辑内容
      albumCon: [],
      // 歌手
      artist: '',
      // 专辑歌曲
      albumSongList: [],
      myLoveSongList: []
    }
  },
  created() {
    bus.$on('initLoveState', () => {
      this.myLoveSongList = loadMyLoveSongList()
    })
  },
  mounted() {
    this.getAlbumCon()
    this.myLoveSongList = loadMyLoveSongList()
  },
  methods: {
    // 获取专辑内容
    async getAlbumCon() {
      const { data: res } = await getAlbumConByIdAPI(this.id)
      // 专辑内容
      this.albumCon = res.album
      this.artist = this.albumCon.artist.name
      // 专辑歌曲
      this.albumSongList = res.songs
    },
    chooseSong(id) {
      bus.$emit('songId', id)
    },
    open() {
      this.$confirm('"播放全部"将会替换当前的播放列表，是否继续？', '替换播放列表', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        lockScroll: false
      })
        .then(() => {
          // 执行替换当前的播放列表的操作
          // 清空当前播放列表
          clearPlayList()
          // 准备新的列表数据，存入播放列表中
          this.albumSongList.forEach((item) => {
            let obj = {
              id: item.id,
              name: item.name,
              artist: item.ar[0].name,
              time: item.dt
            }
            savePlayList(obj)
          })
          // 重新渲染播放列表数据
          bus.$emit('initPlayList')
        })
        .catch(() => {
          // console.log('取消')
        })
    },
    myLove(row) {
      // 判断是否存在
      const index = this.myLoveSongList.findIndex((item) => {
        return item.id === row.id
      })
      if (index >= 0) {
        // 已存在,删除
        deleteMyLoveSongList(row)
      } else {
        // 不存在，添加
        let obj = {
          id: row.id,
          name: row.name,
          artist: row.ar,
          album: row.al,
          time: row.dt
        }
        saveMyLoveSongList(obj)
      }
      this.myLoveSongList = loadMyLoveSongList()
      // 重新渲染 class
      bus.$emit('initLoveState')
    },
    // 判断歌曲是否已喜欢，动态添加 class
    isLoved(id) {
      const index = this.myLoveSongList.findIndex((item) => {
        return item.id === id
      })
      if (index >= 0) {
        return 'loved'
      }
    }
  },
  watch: {
    $route: {
      handler(newVal) {
        this.id = newVal.query.id
        this.getAlbumCon()
      },
      deep: true
    }
  }
}
</script>

<style lang="less" scoped>
.album-container {
  .album-info {
    .name-box {
      text-align: left;
      padding-top: 30px;
      .type {
        padding: 0 2px;
        font-size: 14px;
        color: #ec4141;
        border: 1px solid #ec4141;
        border-radius: 3px;
      }
      h2 {
        display: inline-block;
        margin-left: 10px;
        font-size: 20.5px;
      }
    }
    .el-image {
      float: left;
      margin: 30px;
    }
    .play-all {
      position: relative;
      margin-top: 10px;
      margin-left: 242px;
      width: 140px;
      height: 32.5px;
      line-height: 32.5px;
      background-color: #ec4141;
      text-align: center;
      border-radius: 16.25px;
      cursor: pointer;
      &:hover {
        background-color: #cc3232;
      }
      .el-icon-caret-right {
        position: absolute;
        top: 50%;
        margin-top: -12px;
        left: 10px;
        font-size: 24px;
        color: #fff;
      }
      span {
        margin: 0;
        font-size: 17px;
        color: #fff;
      }
      .el-icon-plus {
        position: absolute;
        top: 50%;
        margin-top: -8px;
        right: 10px;
        font-size: 16px;
        color: #fef4f4;
      }
    }
    .artist {
      margin-top: 15px;
      text-align: left;
      font-size: 14px;
      span {
        color: #507daf;
        cursor: pointer;
        &:hover {
          color: #0b58b0;
        }
      }
    }
    .time {
      margin-top: 10px;
      text-align: left;
      font-size: 14px;
      span {
        color: #676767;
      }
    }
  }
  // 歌曲列表
  .album-songlist {
    /deep/.el-table__cell {
      padding: 9px 0;
      font-size: 12px;
    }
    .el-icon-star-on {
      position: absolute;
      top: 50%;
      margin-top: -10px;
      left: -43px;
      font-size: 20px;
      color: #e5e5e5;
      cursor: pointer;
    }
    .loved {
      color: #ec4141;
    }
  }
}
</style>
